home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 187_01 / sparse.c < prev    next >
C/C++ Source or Header  |  1985-12-29  |  5KB  |  154 lines

  1. /*@*****************************************************/
  2. /*@                                                    */
  3. /*@ sparse.c is a set of routines to maintain a        */
  4. /*@        sparse matrix in free storage.  The         */
  5. /*@        user must define KEYSIZE for his data.      */
  6. /*@                                                    */
  7. /*@*****************************************************/
  8.  
  9. #include    "misc.mon"
  10.  
  11. #define        TNAMEMX        13        /* max size of name field */
  12. int dummy;
  13. #define KEYSIZE TNAMEMX+sizeof(dummy)
  14.  
  15. #include    "blkdef.mon"
  16.  
  17. /*@********************************************************************** */
  18. /*@                                                                       */
  19. /*@ get_blk retrieves the block with the given key.                       */
  20. /*@                                                                       */
  21. /*@   It returns a pointer to the block, if found or zero, if not found   */
  22. /*@                                                                       */
  23. /*@********************************************************************** */
  24.  
  25. int get_blk(list, key, keylen, to_blk, blklen)
  26. struct ndx *list;            /* start of list */
  27. char *key;                    /* ptr to key for which to search */
  28. int keylen;                    /* length of key */
  29. char *to_blk;                /* ptr to area to receive block */
  30. int blklen;                    /* length of block (for move)  */
  31. {
  32.     while(key_cmp(key, keylen, list->nkey))
  33.         if ((list = list->next) == 0)
  34.             break;                /* not found */
  35.  
  36.     if (list)
  37.         cpyblk(list->blk_ptr, to_blk, blklen);
  38.     
  39.     return list;
  40.  
  41. }
  42.  
  43.  
  44. /*@********************************************************************** */
  45. /*@                                                                       */
  46. /*@ key_cmp compares a key in the ndx entry with a given key.             */
  47. /*@                                                                       */
  48. /*@   It returns zero for a match, non-zero otherwise.                    */
  49. /*@                                                                       */
  50. /*@********************************************************************** */
  51.  
  52. int key_cmp(key, keylen, keyptr)
  53. char *key;                /* ptr to key user's match key */
  54. int keylen;                /* length of key */
  55. char *keyptr;            /* ptr to ndx area key */
  56. {
  57.     while (keylen--)
  58.         if (*key++ != *keyptr++)
  59.             break;
  60.                 
  61.     return ++keylen;
  62.  
  63. }
  64.  
  65.  
  66. /*@********************************************************************** */
  67. /*@                                                                       */
  68. /*@ put_blk writes a given block from the specified area.                 */
  69. /*@                                                                       */
  70. /*@   It returns zero for a failure, non-zero otherwise.                  */
  71. /*@                                                                       */
  72. /*@********************************************************************** */
  73.  
  74. int put_blk(list, key, keylen, from_blk, blklen)
  75. struct ndx *list;            /* place to start search */
  76. char *key;                    /* key value use */
  77. int keylen;                    /* length of key */
  78. char *from_blk;                /* ptr to area from which to copy */
  79. int blklen;                    /* length of block */
  80. {
  81.     struct ndx *prev_ptr;
  82.  
  83.     while (key_cmp(key, keylen, list->nkey)) {
  84.         prev_ptr = list;            /* save for insert */
  85.         if ((list = list->next) == 0)
  86.             break;                    /* not found */
  87.     }
  88.  
  89.     if (list == 0) {
  90.         if (!add_ndx(prev_ptr, key, keylen, from_blk, blklen))
  91.             return FALSE;
  92.     }
  93.     else
  94.         cpyblk(from_blk, list->blk_ptr, blklen);
  95.  
  96.     return TRUE;
  97.  
  98. }
  99.  
  100.  
  101.  
  102. /*@********************************************************************** */
  103. /*@                                                                       */
  104. /*@ add_ndx inserts an index block at the end of the list.                */
  105. /*@      It will initialize it to point to the given block.               */
  106. /*@                                                                       */
  107. /*@   It returns zero for a failure, non-zero otherwise.                  */
  108. /*@                                                                       */
  109. /*@********************************************************************** */
  110.  
  111. int add_ndx(prev_ptr, key, keylen, from_blk, blklen)
  112. struct ndx *prev_ptr;            /* insert after here */
  113. char *key;                        /* key string to use */
  114. int keylen;                        /* length of key */
  115. char *from_blk;                    /* area pointed to by ndx block */
  116. int blklen;                        /* length of block */
  117. {
  118.     char *d;
  119.     struct ndx *p, *b;
  120.  
  121.     if (!(p = (struct ndx *)malloc (sizeof(queue_hd))))
  122.         return FALSE;            /* error */
  123.  
  124.     if (!(b = (char *)malloc (blklen)))
  125.         return FALSE;            /* error */
  126.  
  127.     prev_ptr->next = p;            /* save ptr to ndx entry */
  128.     p->next = 0;                /* new end of list */
  129.     d = p->nkey;                /* copy key info */
  130.     while(keylen--)
  131.         *d++ = *key++;
  132.  
  133.     p->blk_ptr = b;
  134.     cpyblk(from_blk, b, blklen);
  135.  
  136.     return TRUE;
  137.  
  138. }
  139.  
  140. /*@********************************************************************** */
  141. /*@                                                                       */
  142. /*@ cpyblk copys from the user's area to the stored area or vice-versa.   */
  143. /*@                                                                       */
  144. /*@********************************************************************** */
  145.  
  146. int cpyblk(area1, area2, length)
  147. char *area1, *area2;
  148. int length;
  149. {
  150.     while(length--)
  151.         *area2++ = *area1++;
  152.  
  153. }
  154.